package com.nebula.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nebula.common.Const;
import com.nebula.common.ServerResponse;
import com.nebula.pojo.User;
import com.nebula.service.IFolderService;
import com.nebula.service.IUserService;
import com.nebula.vo.NicknameVo;
import com.nebula.vo.PersonalInfomationVo;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<User> login(String username,String password,HttpSession session){
		ServerResponse<User> response = userService.login(username, password);
		if(response.isSuccess()){
			session.setAttribute(Const.USERINFO, response.getData());
		}
		return response;
	}
	
	@RequestMapping(value = "/endecodLogin",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<User> endecodLogin(String username,String password,HttpSession session){
		ServerResponse<User> response = userService.endecodLogin(username, password);
		if(response.isSuccess()){
			session.setAttribute(Const.USERINFO, response.getData());
		}
		return response;
	}
	
	@RequestMapping(value = "/logout",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> logout(HttpSession session){
		session.removeAttribute(Const.USERINFO);
		return ServerResponse.createBySuccess();
	}
	
	@RequestMapping(value = "/register",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> register(User user){
		return userService.register(user);
	}
	
	@RequestMapping(value = "/checkValid",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> checkValid(String str,String type){
		return userService.checkValid(str, type);
	}
	
	@RequestMapping(value = "/usedSize",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse getUsedStorageSpaceByUsername(String username){
		return userService.usedSize(username);
	}
	
	@RequestMapping(value = "/get-nickname",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<NicknameVo> getNickname(HttpSession session){
		User user = (User) session.getAttribute(Const.USERINFO);
		return userService.userInfo(user.getUsername());
	}
	
	@RequestMapping(value = "/set-nickname",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> setNickname(HttpSession session,@RequestParam("nickname") String nickname){
		User user = (User) session.getAttribute(Const.USERINFO);
		return userService.setNickname(user.getUsername(), nickname);
	}
	
	@RequestMapping(value = "/update-groupid",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse updateUserGroupId(HttpSession session,@RequestParam("groupId")Integer groupId){
		User user = (User) session.getAttribute(Const.USERINFO);
		return userService.updateUserGroupId(user.getUsername(), groupId);
	}
	
	@RequestMapping(value = "/check-nickname",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse checkNickname(@RequestParam("nickname")String nickname){
		return userService.checkNickname(nickname);
	}
	
	@RequestMapping(value = "/check-password",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse checkPassword(HttpSession session,@RequestParam("password")String password){
		User user = (User) session.getAttribute(Const.USERINFO);
		return userService.checkPassword(user.getUsername(), password);
	}
	
	@RequestMapping(value = "/change-password",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse change_password(HttpSession session,@RequestParam("password")String password){
		User user = (User) session.getAttribute(Const.USERINFO);
		return userService.change_password(user.getUsername(), password);
	}
	
	@RequestMapping(value = "/check-information",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse checkInformation(HttpSession session,@RequestParam("str")String str
			,@RequestParam("type") String type){
		User user = (User) session.getAttribute(Const.USERINFO);
		return userService.checkInformation(str, user.getUsername(),type);
	}
	
	@RequestMapping(value = "/update-information",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse updateInformation(PersonalInfomationVo personalInfomationVo) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		User user = new User();
		user.setUsername(personalInfomationVo.getUsername());
		user.setSex(personalInfomationVo.getSex());
		user.setEmail(personalInfomationVo.getEmail());
		user.setBirthday(sdf.parse(personalInfomationVo.getBirthday()));
		if(!personalInfomationVo.getNickname().equals("")){user.setNickname(personalInfomationVo.getNickname());}
		if(!personalInfomationVo.getRealname().equals("")){user.setRealname(personalInfomationVo.getRealname());}
		if(!personalInfomationVo.getPhone().equals("")){user.setPhone(personalInfomationVo.getPhone());}
		
		return userService.updateInformation(user);
	}
}
